package com.prs.crm.service.crm.impl;

import java.io.File;
import java.util.Collection;

import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.AbstractDataset;
import org.jfree.data.general.DefaultPieDataset;

import com.prs.crm.context.Constrains;
import com.prs.crm.context.FileUtil;
import com.prs.crm.dao.crm.RivalDao;
import com.prs.crm.domain.Attachment;
import com.prs.crm.domain.crm.Rival;
import com.prs.crm.domain.crm.RivalLevel;
import com.prs.crm.domain.crm.RivalNature;
import com.prs.crm.service.crm.RivalService;
import com.prs.crm.service.impl.BaseServiceImpl;

public class RivalServiceImpl extends BaseServiceImpl<Rival,RivalDao> implements RivalService{

	@Override
	public void saveRival(Rival rival, File file, String fileName) {
		// TODO Auto-generated method stub
		Attachment attach = FileUtil.getInstance().createAttach(file, fileName);
		rival.setAttach(attach);
		this.getDao().save(rival);
	}

	@Override
	public boolean existSameRival(String name) {
		// TODO Auto-generated method stub
		boolean flag = false;
		Collection<Rival> rivals = this.getDao().getRivalByName(name);
		if(rivals.size() != 0) {
			flag = true;
		}
		return flag;
	}

	@Override
	public Collection<?> getReportLevelSource() {
		// TODO Auto-generated method stub
		return this.getDao().getReportLevelSource();
	}

	@Override
	public Collection<?> getReportNatureSource() {
		// TODO Auto-generated method stub
		return this.getDao().getReportNatureSource();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public AbstractDataset fillDataset(Collection<?> dataList, int GraphicsType) {
		// TODO Auto-generated method stub
		AbstractDataset Dataset = null;
		if(GraphicsType == Constrains.REPORT_PIECHART)
			Dataset = new DefaultPieDataset();
		else if(GraphicsType == Constrains.REPORT_BARCHART)
			Dataset = new DefaultCategoryDataset();
		
		for(Object[] o : (Collection<Object[]>)dataList){
			String name = "";
			Number count = 0;
			for(Object value : o){
				if(value != null){
					if(value instanceof RivalLevel){
						name = ((RivalLevel) value).getLevel();
					}else if(value instanceof RivalNature){
						name = ((RivalNature) value).getNature();
					}else if(value instanceof Number) {
						count = (Number)value;
					}
				}
			}
			if(!name.equals("")){
				if(GraphicsType == Constrains.REPORT_PIECHART)
					((DefaultPieDataset)Dataset).setValue(name, count);
				else if(GraphicsType == Constrains.REPORT_BARCHART)
					((DefaultCategoryDataset)Dataset).addValue(count, name, name);
			}
		}
		return Dataset;
	}

	
}
